package org.example;

import org.example.domain.TransferMsg;
import org.example.domain.User;

import java.util.*;

public class Main {

    private static User user = new User("张三","12345","123456",1000);
    private static User user2 = new User("李四","34567","45678",2000);
    private static HashMap<String,User> users = new HashMap<>();

    public static void main(String[] args) {

        users.put(user.getAccountNumber(),user);
        users.put(user2.getAccountNumber(),user2);

        Scanner scanner = new Scanner(System.in);

        while(true){
            //登录
            System.out.println("输入您的账号");
            String number = scanner.next();
            if(users.get(number)==null){
                System.out.println("没有此账号");
                continue;
            }
            System.out.println("输入您的密码");
            String passwrod = scanner.next();
            if(!users.get(number).getPassWord().equals(passwrod)){
                System.out.println("密码错误");
                continue;
            }
            //跳转二级菜单
            SecondaryMenu(users.get(number));
        }
    }

    //二级菜单
    private static void SecondaryMenu(User user) {
        Scanner scanner = new Scanner(System.in);
        //查询转账消息
        for (TransferMsg transferMsg : user.getTransfer()) {
            if(transferMsg.isExpire()){
                continue;
            }
            if(transferMsg.getResults()==0){
                System.out.println("您向"+transferMsg.getReceiver()+"的转账对方还没有接收");
            }
            if(transferMsg.getResults()==1){
                System.out.println("您向"+transferMsg.getReceiver()+"的转账对方已经接收");
                transferMsg.setExpire(true);
            }
            if(transferMsg.getResults()==0){
                System.out.println("您向"+transferMsg.getReceiver()+"的转账对方拒绝接收");
                transferMsg.setExpire(true);
            }
        }
        //查询收账消息
        for (TransferMsg transferMsg : user.getCollect()) {
            System.out.println(transferMsg.getReceiver()+"向您转账"+transferMsg.getAmount()+"元，是否接收");
            System.out.println("1：拒绝。2：接收");
            int i = scanner.nextInt();
            if(i==1){
                //发起人
                User receiver = users.get(transferMsg.getReceiver());
                receiver.setAmount(receiver.getAmount()+transferMsg.getAmount());
                for (TransferMsg msg : receiver.getTransfer()) {
                    if(msg.getId()==transferMsg.getId()){
                        msg.setResults(2);
                    }
                }
                System.out.println("金额已原路退还");
            }else {
                User receiver = users.get(transferMsg.getReceiver());
                user.setAmount(user.getAmount()+transferMsg.getAmount());
                for (TransferMsg msg : receiver.getTransfer()) {
                    if(msg.getId()==transferMsg.getId()){
                        msg.setResults(1);
                    }
                }
                System.out.println("已入账");
            }
        }



        while(true){
            System.out.println("二级菜单选择");
            System.out.println("1、产看个人信息");
            System.out.println("2、存钱");
            System.out.println("3、取钱");
            System.out.println("4、转账");
            System.out.println("5、退出");
            int i = scanner.nextInt();
            switch(i){
                case 1:
                    System.out.println(user); break;
                case 2:saveMoney(user); break;
                case 3:withdrawMoney(user); break;
                case 4:transferMoney(user); break;
                case 5:return;
            }
        }
    }

    private static void transferMoney(User user) {
        System.out.println("输入目标账户");
        Scanner scanner = new Scanner(System.in);
        String accountNumber = scanner.next();
        if(users.get(accountNumber)==null){
            System.out.println("该账户不存在");
            return;
        }
        System.out.println("输入你要转账的金额");
        int money = scanner.nextInt();
        if(money>user.getAmount()){
            System.out.println("你的余额不足");
            return;
        }
        user.setAmount(user.getAmount()-money);
        String id = UUID.randomUUID().toString();
        //创建发起人转账消息对象
        TransferMsg transferMsg = new TransferMsg();
        transferMsg.setOriginator(user.getAccountNumber());
        transferMsg.setReceiver(accountNumber);
        transferMsg.setAmount(money);
        transferMsg.setId(id);
        user.getTransfer().add(transferMsg);
        //创建接收人转账消息对象
        TransferMsg transfer = new TransferMsg();
        transfer.setOriginator(accountNumber);
        transfer.setReceiver(user.getAccountNumber());
        transfer.setAmount(money);
        transfer.setId(id);
        users.get(accountNumber).getCollect().add(transfer);
        System.out.println("转账成功，等待对方接收，若对方不接收，金额将返还");
    }

    //取钱
    private static void withdrawMoney(User user) {
        System.out.println("输入您要取的金额");
        Scanner scanner = new Scanner(System.in);
        int money = scanner.nextInt();
        if(money>user.getAmount()){
            System.out.println("你的余额不足");
            return;
        }
        user.setAmount(user.getAmount()-money);
        System.out.println("取钱成功");
    }

    //存钱方法
    private static void saveMoney(User user) {
        System.out.println("输入您要存的金额");
        Scanner scanner = new Scanner(System.in);
        int money = scanner.nextInt();
        user.setAmount(user.getAmount()+money);
        System.out.println("存钱成功");
    }
}